﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace CentrifugalSealCalc
{
    public class ReportWriter
    {
        private Calc _calc = null;
        int yPixels = 20;

        public ReportWriter(Calc calc)
        {
            _calc = calc;
        }

        private void AddLine()
        {
            yPixels += 20; // y = y + 20;
        }

        public void WritePDF()
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = Application.ProductName;

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            XFont Heading1 = new XFont("Times New Roman", 14, XFontStyle.Bold);
            XFont Heading1U = new XFont("Times New Roman", 14, XFontStyle.Bold | XFontStyle.Underline);
            XFont FontNormal = new XFont("Times New Roman", 11, XFontStyle.Regular);

            // Draw the text
            gfx.DrawString("Results", Heading1U, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.TopCenter);
            gfx.DrawString("Impeller", Heading1, XBrushes.Black, new XRect(0, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            gfx.DrawString(_calc.Impeller.Name, FontNormal, XBrushes.Black, new XRect(100, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString(string.Format("R1:\t{0} m", _calc.Impeller.R1), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString(string.Format("R2:\t{0} m", _calc.Impeller.R2), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            AddLine();
            gfx.DrawString("Expeller", Heading1, XBrushes.Black, new XRect(0, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            gfx.DrawString(_calc.Expeller.Name, FontNormal, XBrushes.Black, new XRect(100, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString(string.Format("R1:\t{0} m", _calc.Expeller.R1), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString(string.Format("R2:\t{0} m", _calc.Expeller.R2), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            AddLine();
            gfx.DrawString("Pinlet - Gauage pressure", Heading1, XBrushes.Black, new XRect(0, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            AddLine();
            gfx.DrawString(string.Format("Pinlet:\t{0} m", _calc.Pinlet/1000), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            //gfx.DrawString(string.Format("Pmax:\t{0} m", .Pmax / 1000), FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString("Conclusion", Heading1, XBrushes.Black, new XRect(0, yPixels, page.Width, page.Height), XStringFormats.TopLeft);
            AddLine();
            gfx.DrawString(_calc.Outputs.Message, FontNormal, XBrushes.Black, new XRect(10, yPixels, page.Width, page.Height), XStringFormats.TopLeft);

            // Save the document...
            string filename = _calc.Impeller.Name + ".pdf";
            document.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }
    }
}